# 玩家角色类 - 继承自角色基类
class_name Player
extends Character

# 玩家特有属性
var level: int = 1
var experience: int = 0
var max_experience: int = 100
var mp: int = 50  # 魔法值
var max_mp: int = 50
var skills: Array = []  # 玩家技能列表
var items: Array = []  # 玩家物品列表


# 初始化玩家角色
func _init(node_ref: Node2D, sprite_ref: Sprite2D = null, player_name: String = "Player"):
	super._init(node_ref, sprite_ref)
	name = player_name
	is_player = true


# 升级逻辑
func level_up():
	level += 1
	max_hp += 20
	current_hp = max_hp
	attack += 2
	defense += 1
	speed += 1
	max_mp += 10
	mp = max_mp
	experience = experience - max_experience
	max_experience = int(max_experience * 1.5)  # 升级所需经验递增

	return level


# 学习技能
func learn_skill(skill_name: String, skill_data: Dictionary):
	var skill = skill_data.duplicate()
	skill["name"] = skill_name
	skills.append(skill)


# 使用技能
func use_skill(skill_index: int, target: Character) -> bool:
	if skill_index < 0 or skill_index >= skills.size():
		return false

	var skill = skills[skill_index]

	# 检查MP是否足够
	if mp < skill.get("mp_cost", 0):
		return false

	# 扣除MP
	mp = max(0, mp - skill.get("mp_cost", 0))

	# 处理技能效果
	if skill.get("type") == "attack":
		var damage = attack + skill.get("power", 0)
		target.take_damage(damage)
		return true
	if skill.get("type") == "heal":
		var heal_amount = skill.get("power", 0)
		self.heal(heal_amount)
		return true

	return false


# 使用物品
func use_item(item_index: int, target: Character = null) -> bool:
	if item_index < 0 or item_index >= items.size():
		return false

	var item = items[item_index]

	# 处理物品效果
	if item.get("type") == "potion":
		var heal_amount = item.get("power", 20)
		if target:
			target.heal(heal_amount)
		else:
			self.heal(heal_amount)

		# 消耗物品
		items.remove_at(item_index)
		return true

	return false


# 重写获取状态信息的方法，添加玩家特有的信息
func get_status_info() -> Dictionary:
	var info = super.get_status_info()
	info["level"] = level
	info["experience"] = experience
	info["max_experience"] = max_experience
	info["mp"] = mp
	info["max_mp"] = max_mp
	info["skills_count"] = skills.size()
	info["items_count"] = items.size()
	return info
